package com.sznaner.tool.util;

import android.content.Context;
import android.content.res.AssetManager;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class StringUtil {

    public static String getCurTime(){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String str  = formatter.format(curDate);
        return str;
    }
    public static String getCurWeek(){
        Locale locale = Locale.getDefault();
        String pattern = "EEEE";
        SimpleDateFormat dateTimeFormat = new SimpleDateFormat(pattern, locale);
        Date date = new Date(System.currentTimeMillis());
        String timeInfo = dateTimeFormat.format(date);
        return timeInfo;
    }

    public static Date getDateWithTimeStr(String timeStr){
        SimpleDateFormat dateFormat =  new  SimpleDateFormat("HH:mm:ss");
        Date date = new Date();
        date.setHours(0);
        date.setMinutes(0);
        date.setSeconds(0);
        try {
            date = dateFormat.parse(timeStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }


    //16转10
    public static String str10ForStr16(String str16){
        if(str16 == null) return "";
        int d = Integer.parseInt(str16,16);
        return String.valueOf(d);
    }


    //从Assets获取json
    public static String getJsonForAssets(String fileName) {
        //将json数据变成字符串
        StringBuilder stringBuilder = new StringBuilder();
        try {
            //获取assets资源管理器
            AssetManager assetManager = NRTool.context().getAssets();
            //通过管理器打开文件并读取
            BufferedReader bf = new BufferedReader(new InputStreamReader(
                    assetManager.open(fileName)));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }
    //从本地路径读取json
    public static String getJsonForPath(String filePath) {
        StringBuilder sb = new StringBuilder();
        File file = new File(filePath);
        try {
            FileInputStream fis = new FileInputStream(file);
            InputStreamReader reader = new InputStreamReader(fis,"UTF-8"); //最后的"GBK"根据文件属性而定，如果不行，改成"UTF-8"试试
            BufferedReader br = new BufferedReader(reader);
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            fis.close();
            br.close();
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

}
